<div class="layui-fluid" xmlns:v-on="http://www.w3.org/1999/xhtml">

    <div class="layui-row">
        <div class="layui-col-xs12">
            <div class="layui-card">
                <div class="layui-card-header">
                    新闻详情
                </div>
                <div class="layui-card newsAdd" style="padding-top: 20px">
                    <form class="layui-form">
                        <div class="layui-form-item">
                            <label class="layui-form-label">标题：</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="title">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">内容：</label>
                            <div class="layui-input-block">
                                <textarea id="detail" style="display: none">
                                </textarea>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">作者：</label>
                            <div class="layui-input-block">
                                <input type="text" class="layui-input" name="author">
                            </div>
                        </div>


                        <div style="color: #FD482C; text-align: center">{{errMsg}}</div>
                        <div class="layui-form-item">
                            <div class="layui-input-block" style="text-align: center">
                                <button class="layui-btn" type="button" v-on:click="ajaxSubmit">提交</button>
                                <button class="layui-btn" type="button" v-on:click="back">返回</button>
                            </div>
                        </div>

                    </form>
                </div>
            </div>

        </div>
    </div>

</div>
<script type="text/javascript">


    var $ = layui.jquery;
    var layer = layui.layer;
    var layeditRef, layeditIndex;
    var newsAdd = new Vue({
        el: '.newsAdd',
        data: {
            errMsg: '',
        },
        methods: {
            /**
             * 修改需要加载当前数据
             */
            loadData: function () {
                window.id = layui.router().search.id;
                if (!id) return;
                $.post("http://localhost:8080/movie/news/getById.do", {id: id}, function (data) {
                    $("input[name='title']").val(data.title);
                    $("input[name='author']").val(data.author);
                    // 异步加载
                    setTimeout(function () {
                        layeditRef.setContent(layeditIndex, data.content, false)
                    }, 1000)
                }, 'json');
            },
            ajaxSubmit: function () {
                var obj = {};
                if (!$("input[name='title']").val()) {
                    this.errMsg = "站内新闻标题不能为空"
                    return;
                }
                obj.title = $("input[name='title']").val();
                obj.author = $("input[name='author']").val();
                obj.content = layeditRef.getContent(layeditIndex);
                if (window.id) {
                    obj.id = window.id;
                }
                $.ajax({
                    url: "http://localhost:8080/movie/news/add.do",
                    type: 'post',
                    data: obj,
                    dataType: 'json',
                    async: 'true',
                    success: function (data) {
                        if (data.code != 0) {
                            alert("保存失败！")
                            return;
                        }
                        alert("保存成功！")
                        location.href = '#/news/query'
                    }
                })

            },
            back: function () {
                location.href = '#/news/query'
            }
        }
    });

    layui.use(['form', 'jquery', 'layedit'], function () {
        var form = layui.form;
        form.render();
        /**
         * 富文本编辑器加载
         */

        layeditRef = layui.layedit;
        layeditIndex = layeditRef.build('detail', {
            tool: [
                'strong', // 加粗
                'italic', // 斜体
                'underline', // 下划线
                'del', // 删除线
                '|', // 分割线
                'left', // 左对齐
                'center', // 居中对齐
                'right', // 右对齐
                'link', //超链接
                'unLink', // 清除链接
                'face', // 表情
                'help', // 帮助
            ]
        });
    });

    // 初始化数据
    newsAdd.loadData();
</script>